package com.ks.pc.controller.asyn;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ks.code.ClassifyCode;
import com.ks.dto.ImgDto;
import com.ks.dto.Product2Dto;
import com.ks.dto.ProductDto;
import com.ks.dto.ResultDto;
import com.ks.dto.UserInfoDto;
import com.ks.pc.controller.BaseController;
import com.ks.pc.service.CommonService;
import com.ks.pc.service.PcService;
import com.ks.pc.service.SysBannerService;
import com.ks.pc.service.SysProductService;
import com.ks.utils.paging.Page;

import net.chinahrd.utils.CollectionKit;

@Controller
@RequestMapping("/pc")
public class PcControllerAsyn extends BaseController {

	@Autowired
	private PcService pcService;
	@Autowired
	private CommonService commonService;
	@Autowired
	private SysProductService sysProductService;
	@Autowired
	private SysBannerService sysBannerService;
	
	/**
	 * 左则导航菜单
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryNavigationLeftData", method = RequestMethod.POST)
	public List<Map<String, Object>> queryNavigationLeftData() {
		return pcService.queryNavigationLeftData();
	}

	/**
	 * 首页4大类产品数据。默认每类8个
	 * 
	 * @param brandId
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryPcHomeData", method = RequestMethod.POST)
	public Map<Integer, List<Product2Dto>> queryPcHomeData(String brandId, String searchTxt, Model model) {
		// commonService.checkSessionTimeout();
		List<Product2Dto> list = CollectionKit.newList();
		// 默认
		if (StringUtils.isBlank(brandId) && StringUtils.isBlank(searchTxt)) {
			list = pcService.queryPcHomeData();
		} else {
			// 首页产品数据-左则导航菜单栏条件点入 PcController.pcHomeView()
			if (StringUtils.isNotBlank(brandId) && StringUtils.isBlank(searchTxt)) {
				list = commonService.queryProductAllByBrandId(Integer.parseInt(brandId));
			}
			// 模糊查询产品
			if (StringUtils.isBlank(brandId) && StringUtils.isNotBlank(searchTxt)) {
				Map<String, Object> param = CollectionKit.newMap();
				param.put("searchTxt", searchTxt);
				list = commonService.queryProductBySearchTxt(param);
			}
		}
		Map<Integer, List<Product2Dto>> rs = commonService.groupByShowIndexClassify(list);
		return rs;
	}

	@ResponseBody
	@RequestMapping(value = "/queryProductAll", method = RequestMethod.POST)
	public Map<String, Object> queryProductAll(int pageNo, Integer classifyId, Integer brandId, Model model) {

		// PcController.pcProductListView() 主页点分类
		List<Product2Dto> rs = commonService.queryProductAllByClassifyId(classifyId);

		int pageSize = 12;// 每页显示的行数
		int countItem = rs.size(); // 总数条数

		Page page = new Page(pageNo, pageSize, countItem);
		List<Product2Dto> limitRs = pagingJava(page, rs);

		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("productDtos", limitRs);
		rsMap.put("totalPage", page.getPageCount());
		rsMap.put("totalRecords", countItem);
		return rsMap;
	}

	/**
	 * 假分页
	 * 
	 * @param page
	 * @param rs
	 * @return
	 */
	private List<Product2Dto> pagingJava(Page page, List<Product2Dto> rs) {
		int pageStart = page.getBegin();
		int pageEnd = page.getEnd() > rs.size() ? rs.size() : page.getEnd();
		List<Product2Dto> limitRs = CollectionKit.newList();
		for (int i = pageStart; i < pageEnd; i++) {
			limitRs.add(rs.get(i));
		}
		return limitRs;
	};

	/**
	 * 通过Id获取产品详细
	 * 
	 * @param model
	 * @param productId
	 * @return
	 */
	// @ResponseBody
	// @RequestMapping(value = "/findProductById", method = RequestMethod.POST)
	// public ProductDto findProductById(Model model, Integer productId) {
	// Map<String, Object> param = new HashMap<String, Object>();
	// param.put("productId", productId);
	// return commonService.findProductById(productId);
	// }

	/**
	 * 在线办理
	 * 
	 * @param userName
	 * @param userPhoneNum
	 * @param userIDCard
	 * @param userStages
	 * @param userNote
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insertOnlineOpt", method = RequestMethod.POST)
	public ResultDto<String> insertOnlineOpt(String userName, String userPhoneNum, String userIDCard, String userStages,
			String userNote) {
		ResultDto<String> result = new ResultDto<String>();
		UserInfoDto dto = new UserInfoDto();
		dto.setUserName(userName);
		dto.setUserPhoneNum(userPhoneNum);
		dto.setUserIDCard(userIDCard);
		dto.setUserStages(userStages);
		dto.setUserNote(userNote);
		result.setType(pcService.saveOnlineOpt(dto));
		return result;
	}


	@ResponseBody
	@RequestMapping(value = "/classifyBrandRel", method = RequestMethod.POST)
	public List<Map<String, Object>> classifyBrandRel() {
		return sysProductService.classifyBrandRel();
	}

	@ResponseBody
	@RequestMapping(value = "/findSlidePhotoByClassifyId", method = RequestMethod.POST)
	public List<ImgDto> findSlidePhotoByClassifyId(Integer classifyId) {
		List<ImgDto> list = sysBannerService.findSlidePhotoByClassifyId(classifyId);
		return list;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductByClassifyId", method = RequestMethod.POST)
	public List<List<ProductDto>> findProductByClassifyId(String brandId, Model model) {
		Map<String, Object> param = new HashMap<String, Object>();
		// 菜单栏条件点入
		if (null != brandId) {
			param.put("brandId", brandId);
		}

		param.put("pageStart", 0);
		param.put("pageSize", 8);

		param.put("classifyId", ClassifyCode.PHONE);
		List<ProductDto> phoneDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.IPAD);
		List<ProductDto> padDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.CAMERA);
		List<ProductDto> cameraDtos = sysProductService.findProductByClassifyId(param);
		param.put("classifyId", ClassifyCode.PC);
		List<ProductDto> pcDtos = sysProductService.findProductByClassifyId(param);

		List<List<ProductDto>> products = new LinkedList<List<ProductDto>>();
		products.add(phoneDtos);
		products.add(padDtos);
		products.add(cameraDtos);
		products.add(pcDtos);

		List<List<ProductDto>> orderProducts = new LinkedList<List<ProductDto>>();
		for (int j = 1; j <= 4; j++) {

			for (int i = 0; i < products.size(); i++) {
				Integer showIndex = products.get(i).get(i).getShowIndexClassify();
				if (showIndex == j) {
					orderProducts.add(products.get(i));
					break;
				}
			}

		}
		return orderProducts;
		// Map<String, Object> rs = new HashMap<String, Object>();
		// rs.put("phoneDtos", phoneDtos);
		// rs.put("cameraDtos", cameraDtos);
		// rs.put("pcDtos", pcDtos);
		// rs.put("padDtos", padDtos);
		// return rs;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductListByClassifyId", method = RequestMethod.POST)
	public Map<String, Object> findProductListByClassifyId(int pageNo, Integer classifyId, Integer brandId,
			String searchTxt, Model model) {

		Map<String, Object> param = new HashMap<String, Object>();
		if (null != classifyId) {
			param.put("classifyId", classifyId);
		}
		if (null != brandId) {
			param.put("brandId", brandId);
		}
		if (null != searchTxt && !("".equals(searchTxt))) {
			param.put("searchTxt", searchTxt);
		}

		int pageSize = 12;// 每页显示的行数
		int countItem = sysProductService.countProductByClassifyId(param);// 总数条数
		Page page = new Page(pageNo, pageSize, countItem);

		param.put("pageStart", page.getBegin());
		param.put("pageSize", pageSize);

		List<ProductDto> rs = sysProductService.findProductByClassifyId(param);

		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("productDtos", rs);
		rsMap.put("totalPage", page.getPageCount());
		rsMap.put("totalRecords", countItem);
		return rsMap;
	}

	@ResponseBody
	@RequestMapping(value = "/findProductById", method = RequestMethod.POST)
	public ProductDto findProductById(Model model, Integer productId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("productId", productId);
		return sysProductService.findProductById(productId);
	}

	@ResponseBody
	@RequestMapping(value = "/insertOnlineOpt2", method = RequestMethod.POST)
	public ResultDto<String> insertOnlineOpt2(UserInfoDto userInfoDto) {
		ResultDto<String> result = new ResultDto<String>();
		result.setType(true);
		return result;
	}

}
